package com.like.hospitalworld;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.like.adapter.SubjectPagerAdapter;
import com.like.entity.DocListResp;
import com.like.entity.EventWrapper;
import com.like.entity.GetCourseListReq;
import com.like.network.RetrofitUtil;
import com.like.network.WindowService;
import com.like.utils.Constants;
import com.like.utils.ImageUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_subject)
public class SubjectActivity extends BaseActivity {

    public static final int REQUEST_DOCTOR_INFO = 100;

    @ViewInject(R.id.pager_subject)
    private ViewPager mViewPager;
    @ViewInject(R.id.radio_video)
    private RadioButton mRadioVideo;
    @ViewInject(R.id.radio_courseware)
    private RadioButton mRadioCourse;
    @ViewInject(R.id.radio_record)
    private RadioButton mRadioRecord;
    @ViewInject(R.id.radio_group)
    private RadioGroup mRadioGroup;

    @ViewInject(R.id.img_header)
    private ImageView mImgHeader;
    @ViewInject(R.id.doc_name)
    private TextView mDocName;
    @ViewInject(R.id.doc_pos)
    private TextView mDocPos;
    @ViewInject(R.id.doc_skills)
    private TextView mDocSkills;

    private SubjectPagerAdapter mAdapter;
    private List<RadioButton> mTabViewList = new ArrayList<>();
    private DocListResp.Doctor mDoctor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new SubjectPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mTabViewList.add(mRadioVideo);
        mTabViewList.add(mRadioCourse);
        mTabViewList.add(mRadioRecord);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mRadioGroup.clearCheck();
                mTabViewList.get(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(sticky = true)
    public void onGetStickyEvent(EventWrapper event) {
        if (EventWrapper.isMatch(event, getClass(), REQUEST_DOCTOR_INFO)) {
            mDoctor = (DocListResp.Doctor) event.data;
            mAdapter.initFragments(mDoctor);
            setupView(mDoctor);
        }
    }

    private void setupView(DocListResp.Doctor doc) {
        mDocPos.setText(doc.hospital + " " + doc.jobTitle + " " + doc.departments);
        mDocSkills.setText(Html.fromHtml(doc.remark));
        mDocName.setText(doc.expertsName);
        ImageUtils.showImage(m.mContext, doc.head, m.getToken(), mImgHeader);
    }

    @Event(value = {R.id.radio_video, R.id.radio_courseware, R.id.radio_record}, type = CompoundButton.OnCheckedChangeListener.class)
    private void videoCheck(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.radio_video:
                if (isChecked)
                    mViewPager.setCurrentItem(0);
                break;
            case R.id.radio_courseware:
                if (isChecked)
                    mViewPager.setCurrentItem(1);
                break;
            case R.id.radio_record:
                if (isChecked)
                    mViewPager.setCurrentItem(2);
                break;
        }
    }

}
